3 * Class for viewing MediaWiki file description pages
7 class ImagePage
extends Article
{
19 var $mExtraDescription = false;
21 protected function newPage( Title
$title ) {
22 // Overload mPage with a file-specific page
23 return new WikiFilePage( $title );
27 * Constructor from a page id
28 * @param $id Int article ID to load
30 public static function newFromID( $id ) {
31 $t = Title
::newFromID( $id );
32 # @todo FIXME: Doesn't inherit right
33 return $t == null ?
null : new self( $t );
34 # return $t == null ? null : new static( $t ); // PHP 5.3
41 public function setFile( $file ) {
42 $this->mPage
->setFile( $file );
43 $this->displayImg
= $file;
44 $this->fileLoaded
= true;
47 protected function loadFile() {
48 if ( $this->fileLoaded
) {
51 $this->fileLoaded
= true;
53 $this->displayImg
= $img = false;
54 wfRunHooks( 'ImagePageFindFile', array( $this, &$img, &$this->displayImg
) );
55 if ( !$img ) { // not set by hook?
56 $img = wfFindFile( $this->getTitle() );
58 $img = wfLocalFile( $this->getTitle() );
61 $this->mPage
->setFile( $img );
62 if ( !$this->displayImg
) { // not set by hook?
63 $this->displayImg
= $img;
65 $this->repo
= $img->getRepo();
69 * Handler for action=render
70 * Include body text only; none of the image extras
72 public function render() {
74 $wgOut->setArticleBodyOnly( true );
78 public function view() {
79 global $wgOut, $wgShowEXIF, $wgRequest, $wgUser;
81 $diff = $wgRequest->getVal( 'diff' );
82 $diffOnly = $wgRequest->getBool( 'diffonly', $wgUser->getOption( 'diffonly' ) );
84 if ( $this->getTitle()->getNamespace() != NS_FILE ||
( isset( $diff ) && $diffOnly ) ) {
85 return parent
::view();
90 if ( $this->getTitle()->getNamespace() == NS_FILE
&& $this->mPage
->getFile()->getRedirected() ) {
91 if ( $this->getTitle()->getDBkey() == $this->mPage
->getFile()->getName() ||
isset( $diff ) ) {
92 // mTitle is the same as the redirect target so ask Article
93 // to perform the redirect for us.
94 $wgRequest->setVal( 'diffonly', 'true' );
95 return parent
::view();
97 // mTitle is not the same as the redirect target so it is
98 // probably the redirect page itself. Fake the redirect symbol
99 $wgOut->setPageTitle( $this->getTitle()->getPrefixedText() );
100 $wgOut->addHTML( $this->viewRedirect( Title
::makeTitle( NS_FILE
, $this->mPage
->getFile()->getName() ),
101 /* $appendSubtitle */ true, /* $forceKnown */ true ) );
102 $this->mPage
->viewUpdates();
107 $this->showRedirectedFromHeader();
109 if ( $wgShowEXIF && $this->displayImg
->exists() ) {
110 // @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
111 $formattedMetadata = $this->displayImg
->formatMetadata();
112 $showmeta = $formattedMetadata !== false;
117 if ( !$diff && $this->displayImg
->exists() ) {
118 $wgOut->addHTML( $this->showTOC( $showmeta ) );
122 $this->openShowImage();
125 # No need to display noarticletext, we use our own message, output in openShowImage()
126 if ( $this->mPage
->getID() ) {
127 # When $wgBetterDirectionality is enabled, NS_FILE is in the user language,
128 # but this section (the actual wikitext) should be in page content language
129 $pageLang = $this->getTitle()->getPageLanguage();
130 $wgOut->addHTML( Xml
::openElement( 'div', array( 'id' => 'mw-imagepage-content',
131 'lang' => $pageLang->getCode(), 'dir' => $pageLang->getDir(),
132 'class' => 'mw-content-'.$pageLang->getDir() ) ) );
134 $wgOut->addHTML( Xml
::closeElement( 'div' ) );
136 # Just need to set the right headers
137 $wgOut->setArticleFlag( true );
138 $wgOut->setPageTitle( $this->getTitle()->getPrefixedText() );
139 $this->mPage
->viewUpdates();
142 # Show shared description, if needed
143 if ( $this->mExtraDescription
) {
144 $fol = wfMessage( 'shareddescriptionfollows' );
145 if ( !$fol->isDisabled() ) {
146 $wgOut->addWikiText( $fol->plain() );
148 $wgOut->addHTML( '<div id="shared-image-desc">' . $this->mExtraDescription
. "</div>\n" );
151 $this->closeShowImage();
152 $this->imageHistory();
153 // TODO: Cleanup the following
155 $wgOut->addHTML( Xml
::element( 'h2',
156 array( 'id' => 'filelinks' ),
157 wfMsg( 'imagelinks' ) ) . "\n" );
159 # @todo FIXME: For some freaky reason, we can't redirect to foreign images.
160 # Yet we return metadata about the target. Definitely an issue in the FileRepo
163 # Allow extensions to add something after the image links
165 wfRunHooks( 'ImagePageAfterImageLinks', array( $this, &$html ) );
167 $wgOut->addHTML( $html );
171 $wgOut->addHTML( Xml
::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ) . "\n" );
172 $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
173 $wgOut->addModules( array( 'mediawiki.action.view.metadata' ) );
176 // Add remote Filepage.css
177 if( !$this->repo
->isLocal() ) {
178 $css = $this->repo
->getDescriptionStylesheetUrl();
180 $wgOut->addStyle( $css );
183 // always show the local local Filepage.css, bug 29277
184 $wgOut->addModuleStyles( 'filepage' );
187 public function getDisplayedFile() {
189 return $this->displayImg
;
195 * @param $metadata Boolean: whether or not to show the metadata link
198 protected function showTOC( $metadata ) {
200 '<li><a href="#file">' . wfMsgHtml( 'file-anchor-link' ) . '</a></li>',
201 '<li><a href="#filehistory">' . wfMsgHtml( 'filehist' ) . '</a></li>',
202 '<li><a href="#filelinks">' . wfMsgHtml( 'imagelinks' ) . '</a></li>',
205 $r[] = '<li><a href="#metadata">' . wfMsgHtml( 'metadata' ) . '</a></li>';
208 wfRunHooks( 'ImagePageShowTOC', array( $this, &$r ) );
210 return '<ul id="filetoc">' . implode( "\n", $r ) . '</ul>';
214 * Make a table with metadata to be shown in the output page.
216 * @todo FIXME: Bad interface, see note on MediaHandler::formatMetadata().
218 * @param $metadata Array: the array containing the EXIF data
219 * @return String The metadata table. This is treated as Wikitext (!)
221 protected function makeMetadataTable( $metadata ) {
222 $r = "<div class=\"mw-imagepage-section-metadata\">";
223 $r .= wfMsgNoTrans( 'metadata-help' );
224 $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
225 foreach ( $metadata as $type => $stuff ) {
226 foreach ( $stuff as $v ) {
227 # @todo FIXME: Why is this using escapeId for a class?!
228 $class = Sanitizer
::escapeId( $v['id'] );
229 if ( $type == 'collapsed' ) {
230 $class .= ' collapsable';
232 $r .= "<tr class=\"$class\">\n";
233 $r .= "<th>{$v['name']}</th>\n";
234 $r .= "<td>{$v['value']}</td>\n</tr>";
237 $r .= "</table>\n</div>\n";
242 * Overloading Article's getContent method.
244 * Omit noarticletext if sharedupload; text will be fetched from the
245 * shared upload server if possible.
247 public function getContent() {
249 if ( $this->mPage
->getFile() && !$this->mPage
->getFile()->isLocal() && 0 == $this->getID() ) {
252 return parent
::getContent();
255 protected function openShowImage() {
256 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
257 $wgLang, $wgEnableUploads;
261 $sizeSel = intval( $wgUser->getOption( 'imagesize' ) );
262 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
263 $sizeSel = User
::getDefaultOption( 'imagesize' );
265 // The user offset might still be incorrect, specially if
266 // $wgImageLimits got changed (see bug #8858).
267 if ( !isset( $wgImageLimits[$sizeSel] ) ) {
268 // Default to the first offset in $wgImageLimits
272 $max = $wgImageLimits[$sizeSel];
274 $maxHeight = $max[1];
275 $sk = $wgUser->getSkin();
276 $dirmark = wfUILang()->getDirMark();
278 if ( $this->displayImg
->exists() ) {
280 $page = $wgRequest->getIntOrNull( 'page' );
281 if ( is_null( $page ) ) {
285 $params = array( 'page' => $page );
287 $width_orig = $this->displayImg
->getWidth( $page );
288 $width = $width_orig;
289 $height_orig = $this->displayImg
->getHeight( $page );
290 $height = $height_orig;
292 $longDesc = wfMsg( 'parentheses', $this->displayImg
->getLongDesc() );
294 wfRunHooks( 'ImageOpenShowImageInlineBefore', array( &$this, &$wgOut ) );
296 if ( $this->displayImg
->allowInlineDisplay() ) {
299 # "Download high res version" link below the image
300 # $msgsize = wfMsgHtml( 'file-info-size', $width_orig, $height_orig, $sk->formatSize( $this->displayImg->getSize() ), $mime );
301 # We'll show a thumbnail of this image
302 if ( $width > $maxWidth ||
$height > $maxHeight ) {
303 # Calculate the thumbnail size.
304 # First case, the limiting factor is the width, not the height.
305 if ( $width / $height >= $maxWidth / $maxHeight ) {
306 $height = round( $height * $maxWidth / $width );
308 # Note that $height <= $maxHeight now.
310 $newwidth = floor( $width * $maxHeight / $height );
311 $height = round( $height * $newwidth / $width );
313 # Note that $height <= $maxHeight now, but might not be identical
314 # because of rounding.
316 $msgbig = wfMsgHtml( 'show-big-image' );
317 $otherSizes = array();
318 foreach ( $wgImageLimits as $size ) {
319 if ( $size[0] < $width_orig && $size[1] < $height_orig &&
320 $size[0] != $width && $size[1] != $height ) {
321 $otherSizes[] = $this->makeSizeLink( $params, $size[0], $size[1] );
324 $msgsmall = wfMessage( 'show-big-image-preview' )->
325 rawParams( $this->makeSizeLink( $params, $width, $height ) )->
327 wfMessage( 'show-big-image-other' )->
328 rawParams( $wgLang->pipeList( $otherSizes ) )->parse();
330 # Image is small enough to show full size on image page
331 $msgsmall = wfMsgExt( 'file-nohires', array( 'parseinline' ) );
334 $params['width'] = $width;
335 $params['height'] = $height;
336 $thumbnail = $this->displayImg
->transform( $params );
339 $anchorclose = '<br />' . $msgsmall;
341 $isMulti = $this->displayImg
->isMultipage() && $this->displayImg
->pageCount() > 1;
343 $wgOut->addHTML( '<table class="multipageimage"><tr><td>' );
348 'alt' => $this->displayImg
->getTitle()->getPrefixedText(),
351 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
352 $thumbnail->toHtml( $options ) .
353 $anchorclose . "</div>\n" );
357 $count = $this->displayImg
->pageCount();
360 $label = $wgOut->parse( wfMsg( 'imgmultipageprev' ), false );
365 array( 'page' => $page - 1 ),
366 array( 'known', 'noclasses' )
368 $thumb1 = $sk->makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
369 array( 'page' => $page - 1 ) );
374 if ( $page < $count ) {
375 $label = wfMsg( 'imgmultipagenext' );
380 array( 'page' => $page +
1 ),
381 array( 'known', 'noclasses' )
383 $thumb2 = $sk->makeThumbLinkObj( $this->getTitle(), $this->displayImg
, $link, $label, 'none',
384 array( 'page' => $page +
1 ) );
392 'name' => 'pageselector',
393 'action' => $wgScript,
394 'onchange' => 'document.pageselector.submit();',
397 for ( $i = 1; $i <= $count; $i++
) {
398 $options[] = Xml
::option( $wgLang->formatNum( $i ), $i, $i == $page );
400 $select = Xml
::tags( 'select',
401 array( 'id' => 'pageselector', 'name' => 'page' ),
402 implode( "\n", $options ) );
405 '</td><td><div class="multipageimagenavbox">' .
406 Xml
::openElement( 'form', $formParams ) .
407 Html
::hidden( 'title', $this->getTitle()->getPrefixedDBkey() ) .
408 wfMsgExt( 'imgmultigoto', array( 'parseinline', 'replaceafter' ), $select ) .
409 Xml
::submitButton( wfMsg( 'imgmultigo' ) ) .
410 Xml
::closeElement( 'form' ) .
411 "<hr />$thumb1\n$thumb2<br clear=\"all\" /></div></td></tr></table>"
415 # if direct link is allowed but it's not a renderable image, show an icon.
416 if ( $this->displayImg
->isSafeFile() ) {
417 $icon = $this->displayImg
->iconThumb();
419 $wgOut->addHTML( '<div class="fullImageLink" id="file">' .
420 $icon->toHtml( array( 'file-link' => true ) ) .
428 $filename = wfEscapeWikiText( $this->displayImg
->getName() );
429 $linktext = $filename;
430 if ( isset( $msgbig ) ) {
431 $linktext = wfEscapeWikiText( $msgbig );
433 $medialink = "[[Media:$filename|$linktext]]";
435 if ( !$this->displayImg
->isSafeFile() ) {
436 $warning = wfMsgNoTrans( 'mediawarning' );
437 $wgOut->addWikiText( <<<EOT
438 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span>$dirmark <span class="fileInfo">$longDesc</span></div>
439 <div class="mediaWarning">$warning</div>
443 $wgOut->addWikiText( <<<EOT
444 <div class="fullMedia">{$medialink}{$dirmark} <span class="fileInfo">$longDesc</span>
451 if ( !$this->displayImg
->isLocal() ) {
452 $this->printSharedImageText();
455 # Image does not exist
456 if ( $wgEnableUploads && $wgUser->isAllowed( 'upload' ) ) {
457 // Only show an upload link if the user can upload
458 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
460 'filepage-nofile-link',
461 $uploadTitle->getFullURL( array( 'wpDestFile' => $this->mPage
->getFile()->getName() ) )
464 $nofile = 'filepage-nofile';
466 // Note, if there is an image description page, but
467 // no image, then this setRobotPolicy is overriden
468 // by Article::View().
469 $wgOut->setRobotPolicy( 'noindex,nofollow' );
470 $wgOut->wrapWikiMsg( "<div id='mw-imagepage-nofile' class='plainlinks'>\n$1\n</div>", $nofile );
471 if ( !$this->getID() ) {
472 // If there is no image, no shared image, and no description page,
473 // output a 404, to be consistent with articles.
474 $wgRequest->response()->header( 'HTTP/1.1 404 Not Found' );
480 * Creates an thumbnail of specified size and returns an HTML link to it
481 * @param array $params Scaler parameters
485 private function makeSizeLink( $params, $width, $height ) {
486 $params['width'] = $width;
487 $params['height'] = $height;
488 $thumbnail = $this->displayImg
->transform( $params );
489 if ( $thumbnail && !$thumbnail->isError() ) {
490 return Html
::rawElement( 'a', array(
491 'href' => $thumbnail->getUrl(),
492 'class' => 'mw-thumbnail-link'
493 ), wfMessage( 'show-big-image-size' )->numParams(
494 $thumbnail->getWidth(), $thumbnail->getHeight()
502 * Show a notice that the file is from a shared repository
504 protected function printSharedImageText() {
509 $descUrl = $this->mPage
->getFile()->getDescriptionUrl();
510 $descText = $this->mPage
->getFile()->getDescriptionText();
512 /* Add canonical to head if there is no local page for this shared file */
513 if( $descUrl && $this->mPage
->getID() == 0 ) {
514 $wgOut->addLink( array( 'rel' => 'canonical', 'href' => $descUrl ) );
517 $wrap = "<div class=\"sharedUploadNotice\">\n$1\n</div>\n";
518 $repo = $this->mPage
->getFile()->getRepo()->getDisplayName();
520 if ( $descUrl && $descText && wfMsgNoTrans( 'sharedupload-desc-here' ) !== '-' ) {
521 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-here', $repo, $descUrl ) );
522 } elseif ( $descUrl && wfMsgNoTrans( 'sharedupload-desc-there' ) !== '-' ) {
523 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload-desc-there', $repo, $descUrl ) );
525 $wgOut->wrapWikiMsg( $wrap, array( 'sharedupload', $repo ), ''/*BACKCOMPAT*/ );
529 $this->mExtraDescription
= $descText;
533 public function getUploadUrl() {
535 $uploadTitle = SpecialPage
::getTitleFor( 'Upload' );
536 return $uploadTitle->getFullURL( array(
537 'wpDestFile' => $this->mPage
->getFile()->getName(),
543 * Print out the various links at the bottom of the image page, e.g. reupload,
544 * external editing (and instructions link) etc.
546 protected function uploadLinksBox() {
547 global $wgUser, $wgOut, $wgEnableUploads, $wgUseExternalEditor;
549 if ( !$wgEnableUploads ) {
554 if ( !$this->mPage
->getFile()->isLocal() ) {
558 $sk = $wgUser->getSkin();
560 $wgOut->addHTML( "<br /><ul>\n" );
562 # "Upload a new version of this file" link
563 if ( UploadBase
::userCanReUpload( $wgUser, $this->mPage
->getFile()->name
) ) {
564 $ulink = $sk->makeExternalLink( $this->getUploadUrl(), wfMsg( 'uploadnewversion-linktext' ) );
565 $wgOut->addHTML( "<li id=\"mw-imagepage-reupload-link\"><div class=\"plainlinks\">{$ulink}</div></li>\n" );
568 # External editing link
569 if ( $wgUseExternalEditor ) {
572 wfMsgHtml( 'edit-externally' ),
576 'externaledit' => 'true',
579 array( 'known', 'noclasses' )
582 '<li id="mw-imagepage-edit-external">' . $elink . ' <small>' .
583 wfMsgExt( 'edit-externally-help', array( 'parseinline' ) ) .
588 $wgOut->addHTML( "</ul>\n" );
591 protected function closeShowImage() { } # For overloading
594 * If the page we've just displayed is in the "Image" namespace,
595 * we follow it with an upload history of the image and its usage.
597 protected function imageHistory() {
601 $pager = new ImageHistoryPseudoPager( $this );
602 $wgOut->addHTML( $pager->getBody() );
603 $wgOut->preventClickjacking( $pager->getPreventClickjacking() );
605 $this->mPage
->getFile()->resetHistory(); // free db resources
607 # Exist check because we don't want to show this on pages where an image
608 # doesn't exist along with the noimage message, that would suck. -ævar
609 if ( $this->mPage
->getFile()->exists() ) {
610 $this->uploadLinksBox();
614 protected function queryImageLinks( $target, $limit ) {
615 $dbr = wfGetDB( DB_SLAVE
);
618 array( 'imagelinks', 'page' ),
619 array( 'page_namespace', 'page_title', 'page_is_redirect', 'il_to' ),
620 array( 'il_to' => $target, 'il_from = page_id' ),
622 array( 'LIMIT' => $limit +
1 )
626 protected function imageLinks() {
627 global $wgUser, $wgOut, $wgLang;
631 $res = $this->queryImageLinks( $this->getTitle()->getDbKey(), $limit +
1);
633 $redirects = array();
634 foreach ( $res as $row ) {
635 if ( $row->page_is_redirect
) {
636 $redirects[$row->page_title
] = array();
640 $count = count( $rows );
642 $hasMore = $count > $limit;
643 if ( !$hasMore && count( $redirects ) ) {
644 $res = $this->queryImageLinks( array_keys( $redirects ),
645 $limit - count( $rows ) +
1 );
646 foreach ( $res as $row ) {
647 $redirects[$row->il_to
][] = $row;
650 $hasMore = ( $res->numRows() +
count( $rows ) ) > $limit;
655 Html
::rawElement( 'div',
656 array( 'id' => 'mw-imagepage-nolinkstoimage' ), "\n$1\n" ),
662 $wgOut->addHTML( "<div id='mw-imagepage-section-linkstoimage'>\n" );
664 $wgOut->addWikiMsg( 'linkstoimage', $count );
666 // More links than the limit. Add a link to [[Special:Whatlinkshere]]
667 $wgOut->addWikiMsg( 'linkstoimage-more',
668 $wgLang->formatNum( $limit ),
669 $this->getTitle()->getPrefixedDBkey()
674 Html
::openElement( 'ul',
675 array( 'class' => 'mw-imagepage-linkstoimage' ) ) . "\n"
677 $sk = $wgUser->getSkin();
680 // Sort the list by namespace:title
681 usort( $rows, array( $this, 'compare' ) );
683 // Create links for every element
685 foreach( $rows as $element ) {
687 if ( $currentCount > $limit ) {
691 $link = $sk->linkKnown( Title
::makeTitle( $element->page_namespace
, $element->page_title
) );
692 if ( !isset( $redirects[$element->page_title
] ) ) {
695 $ul = "<ul class='mw-imagepage-redirectstofile'>\n";
696 foreach ( $redirects[$element->page_title
] as $row ) {
698 if ( $currentCount > $limit ) {
702 $link2 = $sk->linkKnown( Title
::makeTitle( $row->page_namespace
, $row->page_title
) );
703 $ul .= Html
::rawElement(
705 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
710 $liContents = wfMessage( 'linkstoimage-redirect' )->rawParams(
711 $link, $ul )->parse();
713 $wgOut->addHTML( Html
::rawElement(
715 array( 'id' => 'mw-imagepage-linkstoimage-ns' . $element->page_namespace
),
721 $wgOut->addHTML( Html
::closeElement( 'ul' ) . "\n" );
724 // Add a links to [[Special:Whatlinkshere]]
725 if ( $count > $limit ) {
726 $wgOut->addWikiMsg( 'morelinkstoimage', $this->getTitle()->getPrefixedDBkey() );
728 $wgOut->addHTML( Html
::closeElement( 'div' ) . "\n" );
731 protected function imageDupes() {
732 global $wgOut, $wgUser, $wgLang;
736 $dupes = $this->mPage
->getDuplicates();
737 if ( count( $dupes ) == 0 ) {
741 $wgOut->addHTML( "<div id='mw-imagepage-section-duplicates'>\n" );
742 $wgOut->addWikiMsg( 'duplicatesoffile',
743 $wgLang->formatNum( count( $dupes ) ), $this->getTitle()->getDBkey()
745 $wgOut->addHTML( "<ul class='mw-imagepage-duplicates'>\n" );
747 $sk = $wgUser->getSkin();
748 foreach ( $dupes as $file ) {
750 if ( $file->isLocal() ) {
756 array( 'known', 'noclasses' )
759 $link = $sk->makeExternalLink( $file->getDescriptionUrl(),
760 $file->getTitle()->getPrefixedText() );
761 $fromSrc = wfMsg( 'shared-repo-from', $file->getRepo()->getDisplayName() );
763 $wgOut->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
765 $wgOut->addHTML( "</ul></div>\n" );
769 * Delete the file, or an earlier version of it
771 public function delete() {
772 global $wgUploadMaintenance;
773 if ( $wgUploadMaintenance && $this->getTitle() && $this->getTitle()->getNamespace() == NS_FILE
) {
775 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
780 if ( !$this->mPage
->getFile()->exists() ||
!$this->mPage
->getFile()->isLocal() ||
$this->mPage
->getFile()->getRedirected() ) {
781 // Standard article deletion
785 $deleter = new FileDeleteForm( $this->mPage
->getFile() );
790 * Revert the file to an earlier version
792 public function revert() {
794 $reverter = new FileRevertForm( $this->mPage
->getFile() );
795 $reverter->execute();
799 * Override handling of action=purge
801 public function doPurge() {
803 if ( $this->mPage
->getFile()->exists() ) {
804 wfDebug( 'ImagePage::doPurge purging ' . $this->mPage
->getFile()->getName() . "\n" );
805 $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' );
807 $this->mPage
->getFile()->upgradeRow();
808 $this->mPage
->getFile()->purgeCache();
810 wfDebug( 'ImagePage::doPurge no image for ' . $this->mPage
->getFile()->getName() . "; limiting purge to cache only\n" );
811 // even if the file supposedly doesn't exist, force any cached information
812 // to be updated (in case the cached information is wrong)
813 $this->mPage
->getFile()->purgeCache();
819 * Display an error with a wikitext description
821 function showError( $description ) {
823 $wgOut->setPageTitle( wfMsg( 'internalerror' ) );
824 $wgOut->setRobotPolicy( 'noindex,nofollow' );
825 $wgOut->setArticleRelated( false );
826 $wgOut->enableClientCache( false );
827 $wgOut->addWikiText( $description );
831 * Callback for usort() to do link sorts by (namespace, title)
832 * Function copied from Title::compare()
834 * @param $a object page to compare with
835 * @param $b object page to compare with
836 * @return Integer: result of string comparison, or namespace comparison
838 protected function compare( $a, $b ) {
839 if ( $a->page_namespace
== $b->page_namespace
) {
840 return strcmp( $a->page_title
, $b->page_title
);
842 return $a->page_namespace
- $b->page_namespace
;
848 * Builds the image revision log shown on image pages
852 class ImageHistoryList
{
867 protected $imagePage;
874 protected $repo, $showThumb;
875 protected $preventClickjacking = false;
878 * @param ImagePage $imagePage
880 public function __construct( $imagePage ) {
881 global $wgUser, $wgShowArchiveThumbnails;
882 $this->skin
= $wgUser->getSkin();
883 $this->current
= $imagePage->getFile();
884 $this->img
= $imagePage->getDisplayedFile();
885 $this->title
= $imagePage->getTitle();
886 $this->imagePage
= $imagePage;
887 $this->showThumb
= $wgShowArchiveThumbnails && $this->img
->canRender();
890 public function getImagePage() {
891 return $this->imagePage
;
894 public function getSkin() {
898 public function getFile() {
902 public function beginImageHistoryList( $navLinks = '' ) {
903 global $wgOut, $wgUser;
904 return Xml
::element( 'h2', array( 'id' => 'filehistory' ), wfMsg( 'filehist' ) ) . "\n"
905 . "<div id=\"mw-imagepage-section-filehistory\">\n"
906 . $wgOut->parse( wfMsgNoTrans( 'filehist-help' ) )
908 . Xml
::openElement( 'table', array( 'class' => 'wikitable filehistory' ) ) . "\n"
910 . ( $this->current
->isLocal() && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ?
'<td></td>' : '' )
911 . '<th>' . wfMsgHtml( 'filehist-datetime' ) . '</th>'
912 . ( $this->showThumb ?
'<th>' . wfMsgHtml( 'filehist-thumb' ) . '</th>' : '' )
913 . '<th>' . wfMsgHtml( 'filehist-dimensions' ) . '</th>'
914 . '<th>' . wfMsgHtml( 'filehist-user' ) . '</th>'
915 . '<th>' . wfMsgHtml( 'filehist-comment' ) . '</th>'
919 public function endImageHistoryList( $navLinks = '' ) {
920 return "</table>\n$navLinks\n</div>\n";
928 public function imageHistoryLine( $iscur, $file ) {
929 global $wgUser, $wgLang;
931 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
932 $img = $iscur ?
$file->getName() : $file->getArchiveName();
933 $user = $file->getUser( 'id' );
934 $usertext = $file->getUser( 'text' );
935 $description = $file->getDescription();
937 $local = $this->current
->isLocal();
938 $row = $selected = '';
941 if ( $local && ( $wgUser->isAllowedAny( 'delete', 'deletedhistory' ) ) ) {
943 # Link to remove from history
944 if ( $wgUser->isAllowed( 'delete' ) ) {
945 $q = array( 'action' => 'delete' );
947 $q['oldimage'] = $img;
949 $row .= $this->skin
->link(
951 wfMsgHtml( $iscur ?
'filehist-deleteall' : 'filehist-deleteone' ),
952 array(), $q, array( 'known' )
955 # Link to hide content. Don't show useless link to people who cannot hide revisions.
956 $canHide = $wgUser->isAllowed( 'deleterevision' );
957 if ( $canHide ||
( $wgUser->isAllowed( 'deletedhistory' ) && $file->getVisibility() ) ) {
958 if ( $wgUser->isAllowed( 'delete' ) ) {
961 // If file is top revision or locked from this user, don't link
962 if ( $iscur ||
!$file->userCan( File
::DELETED_RESTRICTED
) ) {
963 $del = $this->skin
->revDeleteLinkDisabled( $canHide );
965 list( $ts, $name ) = explode( '!', $img, 2 );
967 'type' => 'oldimage',
968 'target' => $this->title
->getPrefixedText(),
971 $del = $this->skin
->revDeleteLink( $query,
972 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
979 // Reversion link/current indicator
982 $row .= wfMsgHtml( 'filehist-current' );
983 } elseif ( $local && $wgUser->isLoggedIn() && $this->title
->userCan( 'edit' ) ) {
984 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
985 $row .= wfMsgHtml( 'filehist-revert' );
987 $row .= $this->skin
->link(
989 wfMsgHtml( 'filehist-revert' ),
992 'action' => 'revert',
994 'wpEditToken' => $wgUser->editToken( $img )
996 array( 'known', 'noclasses' )
1002 // Date/time and image link
1003 if ( $file->getTimestamp() === $this->img
->getTimestamp() ) {
1004 $selected = "class='filehistory-selected'";
1006 $row .= "<td $selected style='white-space: nowrap;'>";
1007 if ( !$file->userCan( File
::DELETED_FILE
) ) {
1008 # Don't link to unviewable files
1009 $row .= '<span class="history-deleted">' . $wgLang->timeAndDate( $timestamp, true ) . '</span>';
1010 } elseif ( $file->isDeleted( File
::DELETED_FILE
) ) {
1012 $this->preventClickjacking();
1013 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
1014 # Make a link to review the image
1015 $url = $this->skin
->link(
1017 $wgLang->timeAndDate( $timestamp, true ),
1020 'target' => $this->title
->getPrefixedText(),
1022 'token' => $wgUser->editToken( $img )
1024 array( 'known', 'noclasses' )
1027 $url = $wgLang->timeAndDate( $timestamp, true );
1029 $row .= '<span class="history-deleted">' . $url . '</span>';
1031 $url = $iscur ?
$this->current
->getUrl() : $this->current
->getArchiveUrl( $img );
1032 $row .= Xml
::element( 'a', array( 'href' => $url ), $wgLang->timeAndDate( $timestamp, true ) );
1037 if ( $this->showThumb
) {
1038 $row .= '<td>' . $this->getThumbForLine( $file ) . '</td>';
1041 // Image dimensions + size
1043 $row .= htmlspecialchars( $file->getDimensionsString() );
1044 $row .= ' <span style="white-space: nowrap;">(' . $this->skin
->formatSize( $file->getSize() ) . ')</span>';
1049 // Hide deleted usernames
1050 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1051 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1054 $row .= $this->skin
->userLink( $user, $usertext ) . ' <span style="white-space: nowrap;">' .
1055 $this->skin
->userToolLinks( $user, $usertext ) . '</span>';
1057 $row .= htmlspecialchars( $usertext );
1060 $row .= '</td><td>';
1062 // Don't show deleted descriptions
1063 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1064 $row .= '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
1066 $row .= $this->skin
->commentBlock( $description, $this->title
);
1071 wfRunHooks( 'ImagePageFileHistoryLine', array( $this, $file, &$row, &$rowClass ) );
1072 $classAttr = $rowClass ?
" class='$rowClass'" : '';
1074 return "<tr{$classAttr}>{$row}</tr>\n";
1081 protected function getThumbForLine( $file ) {
1084 if ( $file->allowInlineDisplay() && $file->userCan( File
::DELETED_FILE
) && !$file->isDeleted( File
::DELETED_FILE
) ) {
1089 $timestamp = wfTimestamp( TS_MW
, $file->getTimestamp() );
1091 $thumbnail = $file->transform( $params );
1093 'alt' => wfMsg( 'filehist-thumbtext',
1094 $wgLang->timeAndDate( $timestamp, true ),
1095 $wgLang->date( $timestamp, true ),
1096 $wgLang->time( $timestamp, true ) ),
1097 'file-link' => true,
1100 if ( !$thumbnail ) {
1101 return wfMsgHtml( 'filehist-nothumb' );
1104 return $thumbnail->toHtml( $options );
1106 return wfMsgHtml( 'filehist-nothumb' );
1110 protected function preventClickjacking( $enable = true ) {
1111 $this->preventClickjacking
= $enable;
1114 public function getPreventClickjacking() {
1115 return $this->preventClickjacking
;
1119 class ImageHistoryPseudoPager
extends ReverseChronologicalPager
{
1120 protected $preventClickjacking = false;
1133 * @param ImagePage $imagePage
1135 function __construct( $imagePage ) {
1136 parent
::__construct();
1137 $this->mImagePage
= $imagePage;
1138 $this->mTitle
= clone ( $imagePage->getTitle() );
1139 $this->mTitle
->setFragment( '#filehistory' );
1141 $this->mHist
= array();
1142 $this->mRange
= array( 0, 0 ); // display range
1145 function getTitle() {
1146 return $this->mTitle
;
1149 function getQueryInfo() {
1153 function getIndexField() {
1157 function formatRow( $row ) {
1161 function getBody() {
1164 if ( count( $this->mHist
) ) {
1165 $list = new ImageHistoryList( $this->mImagePage
);
1166 # Generate prev/next links
1167 $navLink = $this->getNavigationBar();
1168 $s = $list->beginImageHistoryList( $navLink );
1169 // Skip rows there just for paging links
1170 for ( $i = $this->mRange
[0]; $i <= $this->mRange
[1]; $i++
) {
1171 $file = $this->mHist
[$i];
1172 $s .= $list->imageHistoryLine( !$file->isOld(), $file );
1174 $s .= $list->endImageHistoryList( $navLink );
1176 if ( $list->getPreventClickjacking() ) {
1177 $this->preventClickjacking();
1183 function doQuery() {
1184 if ( $this->mQueryDone
) {
1187 $this->mImg
= $this->mImagePage
->getFile(); // ensure loading
1188 if ( !$this->mImg
->exists() ) {
1191 $queryLimit = $this->mLimit +
1; // limit plus extra row
1192 if ( $this->mIsBackwards
) {
1193 // Fetch the file history
1194 $this->mHist
= $this->mImg
->getHistory( $queryLimit, null, $this->mOffset
, false );
1195 // The current rev may not meet the offset/limit
1196 $numRows = count( $this->mHist
);
1197 if ( $numRows <= $this->mLimit
&& $this->mImg
->getTimestamp() > $this->mOffset
) {
1198 $this->mHist
= array_merge( array( $this->mImg
), $this->mHist
);
1201 // The current rev may not meet the offset
1202 if ( !$this->mOffset ||
$this->mImg
->getTimestamp() < $this->mOffset
) {
1203 $this->mHist
[] = $this->mImg
;
1205 // Old image versions (fetch extra row for nav links)
1206 $oiLimit = count( $this->mHist
) ?
$this->mLimit
: $this->mLimit +
1;
1207 // Fetch the file history
1208 $this->mHist
= array_merge( $this->mHist
,
1209 $this->mImg
->getHistory( $oiLimit, $this->mOffset
, null, false ) );
1211 $numRows = count( $this->mHist
); // Total number of query results
1213 # Index value of top item in the list
1214 $firstIndex = $this->mIsBackwards ?
1215 $this->mHist
[$numRows - 1]->getTimestamp() : $this->mHist
[0]->getTimestamp();
1216 # Discard the extra result row if there is one
1217 if ( $numRows > $this->mLimit
&& $numRows > 1 ) {
1218 if ( $this->mIsBackwards
) {
1219 # Index value of item past the index
1220 $this->mPastTheEndIndex
= $this->mHist
[0]->getTimestamp();
1221 # Index value of bottom item in the list
1222 $lastIndex = $this->mHist
[1]->getTimestamp();
1224 $this->mRange
= array( 1, $numRows - 1 );
1226 # Index value of item past the index
1227 $this->mPastTheEndIndex
= $this->mHist
[$numRows - 1]->getTimestamp();
1228 # Index value of bottom item in the list
1229 $lastIndex = $this->mHist
[$numRows - 2]->getTimestamp();
1231 $this->mRange
= array( 0, $numRows - 2 );
1234 # Setting indexes to an empty string means that they will be
1235 # omitted if they would otherwise appear in URLs. It just so
1236 # happens that this is the right thing to do in the standard
1237 # UI, in all the relevant cases.
1238 $this->mPastTheEndIndex
= '';
1239 # Index value of bottom item in the list
1240 $lastIndex = $this->mIsBackwards ?
1241 $this->mHist
[0]->getTimestamp() : $this->mHist
[$numRows - 1]->getTimestamp();
1243 $this->mRange
= array( 0, $numRows - 1 );
1248 $this->mPastTheEndIndex
= '';
1250 if ( $this->mIsBackwards
) {
1251 $this->mIsFirst
= ( $numRows < $queryLimit );
1252 $this->mIsLast
= ( $this->mOffset
== '' );
1253 $this->mLastShown
= $firstIndex;
1254 $this->mFirstShown
= $lastIndex;
1256 $this->mIsFirst
= ( $this->mOffset
== '' );
1257 $this->mIsLast
= ( $numRows < $queryLimit );
1258 $this->mLastShown
= $lastIndex;
1259 $this->mFirstShown
= $firstIndex;
1261 $this->mQueryDone
= true;
1264 protected function preventClickjacking( $enable = true ) {
1265 $this->preventClickjacking
= $enable;
1268 public function getPreventClickjacking() {
1269 return $this->preventClickjacking
;